﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace QLNHK.GenKey
{
	public partial class MaKichHoat : ICloneable
	{
		#region Properties.
		
		public Guid ID { set; get; }
		public Guid IDDonVi { set; get; }
		public string MaMay { set; get; }
		public string MaKichHoatSanPham { set; get; }
		public string TenNguoiSuDung { set; get; }
		public DateTime NgayTao { set; get; }
		public DateTime NgayHetHan { set; get; }
		public int SoNgaySuDung { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static List<MaKichHoat> ConvertToCollection(IDataReader reader)
		{
			List<MaKichHoat> collection = new List<MaKichHoat>();
			while (reader.Read())
			{
				MaKichHoat entity = new MaKichHoat();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetGuid(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("IDDonVi"))) entity.IDDonVi = reader.GetGuid(reader.GetOrdinal("IDDonVi"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaMay"))) entity.MaMay = reader.GetString(reader.GetOrdinal("MaMay"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaKichHoatSanPham"))) entity.MaKichHoatSanPham = reader.GetString(reader.GetOrdinal("MaKichHoatSanPham"));
				if (!reader.IsDBNull(reader.GetOrdinal("TenNguoiSuDung"))) entity.TenNguoiSuDung = reader.GetString(reader.GetOrdinal("TenNguoiSuDung"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayTao"))) entity.NgayTao = reader.GetDateTime(reader.GetOrdinal("NgayTao"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayHetHan"))) entity.NgayHetHan = reader.GetDateTime(reader.GetOrdinal("NgayHetHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoNgaySuDung"))) entity.SoNgaySuDung = reader.GetInt32(reader.GetOrdinal("SoNgaySuDung"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		public static bool Find(List<MaKichHoat> collection, Guid id)
        {
            foreach (MaKichHoat item in collection)
            {
                if (item.ID == id)
                {
                    return true;
                }
            }

            return false;
        }
		
		public static void UpdateDataSet(DataSet ds)
        {
            string insert = "Insert INTO t_MaKichHoat VALUES(@ID, @IDDonVi, @MaMay, @MaKichHoatSanPham, @TenNguoiSuDung, @NgayTao, @NgayHetHan, @SoNgaySuDung)";
            string update = "UPDATE t_MaKichHoat SET IDDonVi = @IDDonVi, MaMay = @MaMay, MaKichHoatSanPham = @MaKichHoatSanPham, TenNguoiSuDung = @TenNguoiSuDung, NgayTao = @NgayTao, NgayHetHan = @NgayHetHan, SoNgaySuDung = @SoNgaySuDung WHERE ID = @ID";
            string delete = "DELETE FROM t_MaKichHoat WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, "IDDonVi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@MaMay", SqlDbType.NVarChar, "MaMay", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, "MaKichHoatSanPham", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, "TenNguoiSuDung", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayTao", SqlDbType.DateTime, "NgayTao", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayHetHan", SqlDbType.DateTime, "NgayHetHan", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoNgaySuDung", SqlDbType.Int, "SoNgaySuDung", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, "IDDonVi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@MaMay", SqlDbType.NVarChar, "MaMay", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, "MaKichHoatSanPham", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, "TenNguoiSuDung", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayTao", SqlDbType.DateTime, "NgayTao", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayHetHan", SqlDbType.DateTime, "NgayHetHan", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoNgaySuDung", SqlDbType.Int, "SoNgaySuDung", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, ds.Tables[0].TableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }
				
		public static void UpdateDataSet(DataSet ds, string tableName)
        {
            string insert = "Insert INTO t_MaKichHoat VALUES(@ID, @IDDonVi, @MaMay, @MaKichHoatSanPham, @TenNguoiSuDung, @NgayTao, @NgayHetHan, @SoNgaySuDung)";
            string update = "UPDATE t_MaKichHoat SET IDDonVi = @IDDonVi, MaMay = @MaMay, MaKichHoatSanPham = @MaKichHoatSanPham, TenNguoiSuDung = @TenNguoiSuDung, NgayTao = @NgayTao, NgayHetHan = @NgayHetHan, SoNgaySuDung = @SoNgaySuDung WHERE ID = @ID";
            string delete = "DELETE FROM t_MaKichHoat WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, "IDDonVi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@MaMay", SqlDbType.NVarChar, "MaMay", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, "MaKichHoatSanPham", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, "TenNguoiSuDung", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayTao", SqlDbType.DateTime, "NgayTao", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayHetHan", SqlDbType.DateTime, "NgayHetHan", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoNgaySuDung", SqlDbType.Int, "SoNgaySuDung", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, "IDDonVi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@MaMay", SqlDbType.NVarChar, "MaMay", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, "MaKichHoatSanPham", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, "TenNguoiSuDung", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayTao", SqlDbType.DateTime, "NgayTao", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayHetHan", SqlDbType.DateTime, "NgayHetHan", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoNgaySuDung", SqlDbType.Int, "SoNgaySuDung", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.UniqueIdentifier, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, tableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }

		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static MaKichHoat Load(Guid id)
		{
			const string spName = "[dbo].[p_MaKichHoat_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, id);
            IDataReader reader = db.ExecuteReader(dbCommand);
			List<MaKichHoat> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_MaKichHoat_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_MaKichHoat_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_MaKichHoat_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_MaKichHoat_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertMaKichHoat(Guid iD, Guid iDDonVi, string maMay, string maKichHoatSanPham, string tenNguoiSuDung, DateTime ngayTao, DateTime ngayHetHan, int soNgaySuDung)
		{
			MaKichHoat entity = new MaKichHoat();	
            entity.ID = iD;
			entity.IDDonVi = iDDonVi;
			entity.MaMay = maMay;
			entity.MaKichHoatSanPham = maKichHoatSanPham;
			entity.TenNguoiSuDung = tenNguoiSuDung;
			entity.NgayTao = ngayTao;
			entity.NgayHetHan = ngayHetHan;
			entity.SoNgaySuDung = soNgaySuDung;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_MaKichHoat_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			db.AddInParameter(dbCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, IDDonVi);
			db.AddInParameter(dbCommand, "@MaMay", SqlDbType.NVarChar, MaMay);
			db.AddInParameter(dbCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, MaKichHoatSanPham);
			db.AddInParameter(dbCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, TenNguoiSuDung);
			db.AddInParameter(dbCommand, "@NgayTao", SqlDbType.DateTime, NgayTao.Year <= 1753 ? DBNull.Value : (object) NgayTao);
			db.AddInParameter(dbCommand, "@NgayHetHan", SqlDbType.DateTime, NgayHetHan.Year <= 1753 ? DBNull.Value : (object) NgayHetHan);
			db.AddInParameter(dbCommand, "@SoNgaySuDung", SqlDbType.Int, SoNgaySuDung);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateMaKichHoat(Guid id, Guid iDDonVi, string maMay, string maKichHoatSanPham, string tenNguoiSuDung, DateTime ngayTao, DateTime ngayHetHan, int soNgaySuDung)
		{
			MaKichHoat entity = new MaKichHoat();			
			entity.ID = id;
			entity.IDDonVi = iDDonVi;
			entity.MaMay = maMay;
			entity.MaKichHoatSanPham = maKichHoatSanPham;
			entity.TenNguoiSuDung = tenNguoiSuDung;
			entity.NgayTao = ngayTao;
			entity.NgayHetHan = ngayHetHan;
			entity.SoNgaySuDung = soNgaySuDung;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_MaKichHoat_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			db.AddInParameter(dbCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, IDDonVi);
			db.AddInParameter(dbCommand, "@MaMay", SqlDbType.NVarChar, MaMay);
			db.AddInParameter(dbCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, MaKichHoatSanPham);
			db.AddInParameter(dbCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, TenNguoiSuDung);
			db.AddInParameter(dbCommand, "@NgayTao", SqlDbType.DateTime, NgayTao.Year <= 1753 ? DBNull.Value : (object) NgayTao);
			db.AddInParameter(dbCommand, "@NgayHetHan", SqlDbType.DateTime, NgayHetHan.Year <= 1753 ? DBNull.Value : (object) NgayHetHan);
			db.AddInParameter(dbCommand, "@SoNgaySuDung", SqlDbType.Int, SoNgaySuDung);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateMaKichHoat(Guid id, Guid iDDonVi, string maMay, string maKichHoatSanPham, string tenNguoiSuDung, DateTime ngayTao, DateTime ngayHetHan, int soNgaySuDung)
		{
			MaKichHoat entity = new MaKichHoat();			
			entity.ID = id;
			entity.IDDonVi = iDDonVi;
			entity.MaMay = maMay;
			entity.MaKichHoatSanPham = maKichHoatSanPham;
			entity.TenNguoiSuDung = tenNguoiSuDung;
			entity.NgayTao = ngayTao;
			entity.NgayHetHan = ngayHetHan;
			entity.SoNgaySuDung = soNgaySuDung;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_MaKichHoat_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			db.AddInParameter(dbCommand, "@IDDonVi", SqlDbType.UniqueIdentifier, IDDonVi);
			db.AddInParameter(dbCommand, "@MaMay", SqlDbType.NVarChar, MaMay);
			db.AddInParameter(dbCommand, "@MaKichHoatSanPham", SqlDbType.NVarChar, MaKichHoatSanPham);
			db.AddInParameter(dbCommand, "@TenNguoiSuDung", SqlDbType.NVarChar, TenNguoiSuDung);
			db.AddInParameter(dbCommand, "@NgayTao", SqlDbType.DateTime, NgayTao.Year <= 1753 ? DBNull.Value : (object) NgayTao);
			db.AddInParameter(dbCommand, "@NgayHetHan", SqlDbType.DateTime, NgayHetHan.Year <= 1753 ? DBNull.Value : (object) NgayHetHan);
			db.AddInParameter(dbCommand, "@SoNgaySuDung", SqlDbType.Int, SoNgaySuDung);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteMaKichHoat(Guid id)
		{
			MaKichHoat entity = new MaKichHoat();
			entity.ID = id;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_MaKichHoat_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.UniqueIdentifier, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_MaKichHoat_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		
        #region ICloneable Members

        public object Clone()
        {
            return base.MemberwiseClone();
        }

        #endregion
	}	
}